﻿<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="100px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>活动信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_ActivityId_FormItem"
								prop="Entity.ActivityId"
								:rules="[{ required: true, message: '活动ID为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="活动ID" tooltip="关联的活动ID" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.ActivityId" clearable class="width100">
									<el-option v-for="item in stateData.AllSysActivityLists" :key="item.Value" :value="item.Value" :label="item.Text"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>参与者信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_ParticipantType_FormItem"
								prop="Entity.ParticipantType"
								:rules="[{ required: true, message: '参与者类型为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="参与者类型" tooltip="个人/公司" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.ParticipantType" clearable class="width100">
									<el-option key="个人" value="个人" label="个人"></el-option>
									<el-option key="公司" value="公司" label="公司"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_Name_FormItem"
								prop="Entity.Name"
								:rules="[{ required: true, message: '姓名/公司名为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="姓名/公司名" tooltip="参与人姓名或公司名称" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Name" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_Phone_FormItem"
								prop="Entity.Phone"
								:rules="[{ required: true, message: '联系电话为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="联系电话" tooltip="联系电话" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Phone" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Address_FormItem" prop="Entity.Address">
								<template #label>
									<WtmLabelTooltip label="详细地址" tooltip="详细地址" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Address" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>图片信息</span>
					</div>

					<el-row :gutter="24">
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item ref="SelectedActivityImagesIds_FormItem" prop="SelectedActivityImagesIds">
								<template #label>
									<WtmLabelTooltip label="活动图片" tooltip="图片文件" />
								</template>
								<wtm-upload-image
									v-model="stateData.vmModel.SelectedActivityImagesIds"
									:multi="true"
									clearable
									:limit="30"
									@upload-status="handleUploadStatus"
								></wtm-upload-image>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>其他信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Description_FormItem" prop="Entity.Description">
								<template #label>
									<WtmLabelTooltip label="描述" tooltip="描述" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Description" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>系统用户绑定</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_UserId_FormItem" prop="Entity.UserId">
								<template #label>
									<WtmLabelTooltip label="绑定用户" tooltip="关联的系统用户对象" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.UserId" clearable class="width100">
									<el-option v-for="item in stateData.AllFrameworkUsers" :key="item.Value" :value="item.Value" :label="item.Text"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div style="text-align: right">
					<WtmButton
						@click="onSubmit"
						type="primary"
						:button-text="$t('message._system.common.vm.submit')"
						:disabled="loading || isUploading"
						style="margin-top: 15px"
					/>
					<WtmButton @click="onClose" type="default" :button-text="$t('message._system.common.vm.cancel')" style="margin-top: 15px" />
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="message._system.common.vm.add;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted } from 'vue';
import { SysActivityParticipationApi as api } from '/@/views/activity/sysactivityparticipation/api';
import other from '/@/utils/other';
import { ISysActivityParticipationVm } from '/@/views/activity/sysactivityparticipation/api/interface';
import { IViewModel } from '/@/types/common';

const ci = getCurrentInstance() as any;
const loading = ref(false);
const isUploading = ref(false);
// 定义变量内容
const formRef = ref();
const stateData = reactive<IViewModel<ISysActivityParticipationVm>>({
	vmModel: {
		Entity: {
			ActivityId: null,
			ParticipantType: null,
			Name: null,
			Phone: null,
			Address: null,
			Description: null,
			UserId: null,
			ActivityImages: null,
		},
		SelectedActivityImagesIds: [],
	},

	AllSysActivityLists: [] as any[],
	AllFrameworkUsers: [] as any[],
});

// 监听图片上传状态
const handleUploadStatus = (status: boolean) => {
	isUploading.value = status;
};

// 取消
const onClose = () => {
	closeDialog();
};

// 提交
const onSubmit = () => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			api()
				.add(stateData.vmModel)
				.then(() => {
					ElMessage.success(ci.proxy.$t('message._system.common.vm.submittip'));
					emit('refresh');
					closeDialog();
				})
				.catch((error) => {
					other.setFormError(ci, error);
				})
				.finally(() => {
					loading.value = false;
				});
		}
	});
};

// 页面加载时
onMounted(() => {
	loading.value = true;
	if (ci.attrs['wtmdata']) {
		stateData.vmModel.Entity.ID = ci.attrs['wtmdata'].ID;
	}
	const promises: Promise<any>[] = [
		other.getSelectList('/api/SysActivityParticipation/GetSysActivityLists', [], false).then((x) => {
			stateData.AllSysActivityLists = x;
		}),

		other.getSelectList('/api/SysActivityParticipation/GetFrameworkUsers', [], false).then((x) => {
			stateData.AllFrameworkUsers = x;
		}),
	];
	Promise.allSettled(promises).finally(() => {
		loading.value = false;
	});
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh', 'closeDialog']);
// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};
</script>
